package com.example.cart.web;

import com.example.cart.model.Car;
import com.example.cart.service.CarService;
import com.example.cart.utils.MyResult;
import com.example.cart.utils.MyUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CarController {

    @Autowired
    CarService carService;

    @GetMapping(value = "/allCar")
    public List<Car> searchAll(){ return carService.getAll();}

    @GetMapping(value = "/car")
    public Map<String,Object> searchCarsByPage(@RequestParam Map<String,Object> params){
        MyUtils.parsePageParams(params);
        int count = carService.getSearchCount(params); //获得总数
        List<Car> cars = carService.searchCarsByPage(params); //分页查询
        return MyResult.getListResultMap(0,"success",count,cars);
    }
    @PostMapping(value = "/addcar")
    public Integer addCar(@RequestBody Car car){ return carService.addCar(car); }

    @DeleteMapping(value = "/deletecar")
    public Integer deleteCar(@RequestBody Car car){ return carService.deleteCar(car); }

    @RequestMapping(value = "updatecar")
    public Integer updateCar(@RequestBody Car car){return carService.updateCar(car); }
}
